package pl.edu.mimuw.mas.agent.behaviour.deer;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import pl.edu.mimuw.mas.agent.DeerAgent;
import pl.edu.mimuw.mas.agent.behaviour.ServerBehaviour;
import pl.edu.mimuw.mas.message.ScareMessage;

public class FleeServerBehaviour extends ServerBehaviour {

	private static final long serialVersionUID = 5334300280870029521L;
	
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	public FleeServerBehaviour(DeerAgent a) {
		super(a);
		
		MessageTemplate mt = MessageTemplate.and(
				MessageTemplate.MatchConversationId(DeerAgent.SCARE_CONV),
				MessageTemplate.MatchPerformative(ACLMessage.INFORM)
				);
		setMessageTemplate(mt);
	}

	@Override
	public boolean doAction(ACLMessage msg, ACLMessage reply) throws UnreadableException {
		DeerAgent a = (DeerAgent) myAgent;
		
		ScareMessage sm = (ScareMessage) msg.getContentObject();
		if (sm != null) {
			logger.info("{} : nowe źródło straszenia", a.getName());
			a.addFearSource(sm.position, sm.timeout);
		} else {
			logger.error("{} : wiadomość o straszeniu bez źródła i czasu");
		}
		
		return false;
	}

}
